<?php
/**
 * Core services offered
 *
 * @author Ishtiaque Shahrier <md.ishtiaque@newgenbd.net>
 * @package core
 * @subpackage libraries
 */
class service_l_core {
    /**
     * Gets the curent site information by mtaching the server URL info adn the database
     * URL info.If nothing is found an error is triggered.
     * @return array
     */
    public static function get_current_site() {
        $base_url = "://" . $_SERVER['HTTP_HOST'];
        $base_url.= str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
        $ci = get_instance();
        $query =  $ci->db->query("SELECT * FROM sites WHERE URL LIKE '%$base_url' LIMIT 1");

        if(!$query->num_rows()>0){
            show_error("Please see site base url configuration");
        }else{
            return $query->row();
        }        
    }
    /**
     * Get the GLOBAL settings for the entire application
     * Returns std object if theres no settings for the module else settings_m_core class
     * @param bool TRUE will reload all settings
     * @return object
     */
    public static function get_global_settings($force_reload=FALSE){
        static $_global_settings = NULL;
        if($_global_settings===NULL || $force_reload){
        $query = get_instance()->db->get("global_settings");
            if($query->num_rows()>0){
                $_global_settings = new settings_m_core($query->result());
            }else{
                $_global_settings = new stdClass();
            }            
        }
        return $_global_settings;
    }
    /**
     * Get specific GLOBAL setting for the application
     * @param string $name name of the settings exactly in the database table
     * @param bool TRUE will reload all settings
     * @return NULL|string null if the setting does not exist
     */
    public static function get_global_setting($name, $force_reload=FALSE){
        $_global_settings = service_l_core::get_global_settings($force_reload);
        if(property_exists($_global_settings, $name)){
            $setting = $_global_settings->$name;
            if(strlen($setting->value)){
                return $setting->value;
            }else{
                return $setting->default;
            }
        }else{
            return NULL;
        }
    }

    public static function get_current_language(){
        //@TO_DO : there will be a session variable that will override sites default language prefference
        $site = service_l_core::get_current_site();
        $lang = new language_m_core();
        $lang->get_by_id($site->language_id);
        if($lang->exists()){
            return $lang;
        }else{
            $lang->get_by_id(1);
            return $lang;
        }
    }

    public static function get_company(){
        static $company = NULL;
        if($company===NULL){
            $company = new company_m_core();
            $company = $company->get(1);
            if(!$company->exists()){
                show_error("Company information is not set!");
            }
        }
        return $company;

    }
}
?>